% function Phase_out = BPS_up(Fre_out,Constell)
% 盲相位搜索 2024.6.24 By YMZ
clear;
close all
load("./TX_QAM/fre_out.mat");
Constell = 2;
Phase_in=Fre_out;
Phase_in = sqrt((2^Constell-1)/1.5)*Phase_in./sqrt(mean(abs(Phase_in).^2,2));
scatterplot(Phase_in);
B =2^(Constell+1); % 这么多个区间
N=25;
Phase_mat = (pi/2)*(-B/2:B/2-1)/B;
Phase_mat = reshape(Phase_mat,[],1);%%测试相位取值
QAM_symbol=qammod(0:2^Constell-1,2^Constell);
% qam_test_list = repmat(angle(QAM_symbol)',1,length(Phase_in));

qam_test_list=repmat(exp(-1j*Phase_mat),1,length(Phase_in));%%B*N的数组，每一行表示每一种测试相位的序列
qam_result = qam_test_list.*repmat(Phase_in,B,1);

for ii = 1:B
    scatterplot(qam_result(ii,:));
    title("第i组")
end


% Phase_out_mat = AA.*repmat(Phase_in,B,1); %%每一组测试相位调制后的结果
% % Phase_out_mat_1=reshape(Phase_out_mat,1,[]);
% % QAM_symbol=repmat(QAM_symbol.',1,length(Phase_out_mat_1));
% AA=abs(Phase_out_mat-QAM_symbol);
% [~,pos1]=min(AA);
% Phase_out_mat_A=QAM_symbol(pos1);
% Phase_out_mat_A=reshape(Phase_out_mat_A,B,[]);
% 
% nn=1;
% for num_fg = 2*N+1:length(Phase_in)-(2*N+1)
%     Phase_out_mat_A1=Phase_out_mat_A(:,num_fg-2*N:num_fg+2*N);
%     Phase_out_mat1=Phase_out_mat(:,num_fg-2*N:num_fg+2*N);
%     error = abs(Phase_out_mat_A1-Phase_out_mat1);
%     [~,pos]=min(mean(error,2));
%     POS(nn)=pos;
%     if nn>2 && (POS(nn)-POS(nn-1))<-2^(Constell)
%         Phase_mat=Phase_mat+pi/2;
% end
% 
% if nn>2 && (POS(nn)-POS(nn-1))>2^(Constell)
%     Phase_mat=Phase_mat-pi/2;
% end
% nn=nn+1;
% 
% Phase_out(num_fg) = Phase_in(num_fg)*exp(-1j*Phase_mat(pos));
% end
% figure;plot(POS)
% Phase_out=Phase_out-mean(Phase_out);
% scatterplot(Phase_out);
% % scatterplot(Phase_out);
% % figure;plot(Phase_out,'.');